﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeadVinaglue.Models.ViewPages;
using LeadVinaglue.Utility;

namespace LeadVinaglue.Areas.Management.Controllers
{
    public class EmailListController : Controller
    {
        // Create an instance of user manage
        LeadVinaglue.Repository.EmailManagementRepository repository = new LeadVinaglue.Repository.EmailManagementRepository();

        //
        // GET: /Management/EmailList/
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Index()
        {
            EmailManagementModel model = new EmailManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", 0);

            return View(model);
        }

        //
        // GET: /Management/EmailList/Edit/id
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Add()
        {
            EmailManagementModel model = new EmailManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", 0);

            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Add(FormCollection form)
        {
            Repository.EmailTemplateRepository emailRepository = new Repository.EmailTemplateRepository();
            EmailTemplate model = new EmailTemplate(); // emailRepository.GetSingle(id);

            if (model != null)
            {
                model.EmailType = form["EmailType"].ToString();
                model.SubjectLine = form["SubjectLine"].ToString();
                model.FromAddress = form["FromAddress"].ToString();
                model.CC = form["CC"].ToString();
                model.Body = form["BodyText"].ToString();
                model.Active = (form["Active"]!= null && form["Active"].ToString() == "on");

                emailRepository.Create(model);
            }

            return RedirectToAction("Index", "EmailList");
        }
        
        //
        // GET: /Management/EmailList/Edit/id
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Edit(int id)
        {
            EmailManagementModel model = new EmailManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", id);
            ViewBag.EmailTemplateId = id;

            return View(model);
        }

        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult PopupEmail()
        {
            UserManagementModel model = new UserManagementModel();
            model = new LeadVinaglue.Repository.UserManagementRepository().GetModel(Helpers.Constant.HOME, "SuperAdministrator", "ClientAdmin", string.Empty, "ID", "desc");
            return View(model);
        }
        
        //
        // GET: /Management/EmailList/Edit/id
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Preview(int id)
        {
            EmailManagementModel model = new EmailManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", id);
            ViewBag.EmailTemplateId = id;

            return View(model);
        }

        //
        // GET: /Management/EmailList/Edit/id
        [HttpPost]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult SendEmailNotice(int id, string emailaddress)
        {
            Repository.EmailTemplateRepository emailRepository = new Repository.EmailTemplateRepository();
            EmailTemplate model = emailRepository.GetSingle(id);
            string message = string.Empty;
            bool success = false;

            if (model != null)
            {
                string[] addresses = emailaddress.Split(';');

                // Loop on addresses
                // Send an email to user
                if (addresses.Length > 0)
                {
                    foreach (var email in addresses)
                    {
                        if (ValidateInput.IsValidEmailAddress(email))
                        {
                            UserEmailToken.SendEmailNotice(email, model, ref message, ref success);
                        }
                    }
                }
            }

            return Json(message);
        }

        //
        // GET: /Management/EmailList/Edit/id
        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Edit(int id, FormCollection form)
        {
            Repository.EmailTemplateRepository emailRepository = new Repository.EmailTemplateRepository();
            EmailTemplate model = emailRepository.GetSingle(id);

            if (model != null)
            {
                model.EmailType = form["EmailType"].ToString();
                model.SubjectLine = form["SubjectLine"].ToString();
                model.FromAddress = form["FromAddress"].ToString();
                model.CC = form["CC"].ToString();
                model.Body = form["BodyText"].ToString();
                model.Active = (form["Active"] != null && form["Active"].ToString() == "on");

                emailRepository.Update(model);
            }

            return RedirectToAction("Index", "EmailList");
        }

        // Delete Email Item
        [HttpGet]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Delete(int id)
        {
            Repository.EmailTemplateRepository emailRepository = new Repository.EmailTemplateRepository();
            emailRepository.Delete(id);

            EmailManagementModel model = new EmailManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator");

            return Json(model, JsonRequestBehavior.AllowGet);
        }
    }
}
